package com.qf.commerce.controller;

import com.github.pagehelper.PageInfo;
import com.qf.commerce.entity.BalanceTransactions;
import com.qf.commerce.resp.ResponseResult;
import com.qf.commerce.service.BalanceTransactionsService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.math.BigDecimal;

@RestController
@RequestMapping("/bt")
public class BalanceTransactionsController {

    @Resource
    private BalanceTransactionsService balanceTransactionsService;

    /**
     * 分页展示余额变动明细
     *
     * @param userId userId
     * @param page   page
     * @param size   size
     * @return com.qf.commerce.resp.ResponseResult<com.github.pagehelper.PageInfo < com.qf.commerce.entity.BalanceTransactions>>
     * @author 358
     * @since JDK 1.8
     */
    @GetMapping("/list")
    public ResponseResult<PageInfo<BalanceTransactions>> listById(@RequestParam("userId") Integer userId, @RequestParam(name = "page",defaultValue = "1") Integer page, @RequestParam(name = "size",defaultValue = "6") Integer size) {
        try {
            return ResponseResult.success(balanceTransactionsService.listById(userId, page, size));
        } catch (Exception e) {
            return ResponseResult.fail();
        }
    }

    /**
     * 余额增加
     *
     * @param userId  userId
     * @param money   money
     * @param comment comment
     * @return com.qf.commerce.resp.ResponseResult<java.math.BigDecimal>
     * @author 358
     * @since JDK 1.8
     */
    @PostMapping("/balance/add")
    public ResponseResult<BigDecimal> add(Integer userId, BigDecimal money, String comment) {
        try {
            return ResponseResult.success(balanceTransactionsService.add(userId, money, comment));
        } catch (Exception e) {
            return ResponseResult.fail();
        }
    }

    /**
     * 余额减少
     *
     * @param userId  userId
     * @param money   money
     * @param comment comment
     * @return com.qf.commerce.resp.ResponseResult<java.math.BigDecimal>
     * @author 358
     * @since JDK 1.8
     */
    @PostMapping("/balance/subtract")
    public ResponseResult<BigDecimal> subtract(Integer userId, BigDecimal money, String comment) {
        try {
            return ResponseResult.success(balanceTransactionsService.subtract(userId, money, comment));
        } catch (Exception e) {
            return ResponseResult.fail();
        }
    }
}
